package models

import (
	"github.com/jinzhu/gorm"
	"kop/modules/configuration/configuration/attribute"
	"kop/modules/configuration/configuration/upgrade"
	"kop/modules/ring/configuration"
	"kop/modules/ring/entity"
	"kop/pb"
)

func AutoMigrate(dB *gorm.DB) {
	dB.AutoMigrate(new(entity.Ring))
}

func GetRings(dB *gorm.DB, roleID int32) []*pb.Ring {

	var rings []*pb.Ring

	var data []entity.Ring
	var err = dB.Find(&data, "role_id = ?", roleID).Error
	if err != nil {
		panic(err)
	}

	for _, ringEntity := range data {
		var ring = buildPbRing(ringEntity)
		rings = append(rings, &ring)
	}
	return rings
}

func GetByRingID(dB *gorm.DB, roleID, ringID int32) []*pb.Ring {

	var rings []*pb.Ring

	var data []entity.Ring
	if err := dB.Find(&data, "role_id = ? AND ring_id = ?", roleID, ringID).Error; err != nil {
		panic(err)
	}

	for _, ringEntity := range data {
		var ring = buildPbRing(ringEntity)
		rings = append(rings, &ring)
	}
	return rings
}

func GetRing(dB *gorm.DB, roleID, ID int32) (pb.Ring, error) {

	var ringEntity entity.Ring

	var err = dB.Where("role_id=?", roleID).First(&ringEntity, ID).Error
	if err == gorm.ErrRecordNotFound {
		return pb.Ring{}, err
	} else if err != nil {
		panic(err)
	}

	return buildPbRing(ringEntity), nil
}

func Create(dB *gorm.DB, roleID, ringID int32) pb.Ring {

	var ringEntity = entity.Ring{
		RoleID: roleID,
		RingID: ringID,
	}

	if err := dB.Create(&ringEntity).Error; err != nil {
		panic(err)
	}

	return buildPbRing(ringEntity)
}

func Save(dB *gorm.DB, roleID int32, ring *pb.Ring) {

	var ringEntity = entity.Ring{
		ID:          ring.ID,
		RoleID:      roleID,
		RingID:      ring.RingID,
		Level:       ring.Level,
		Element1:    ring.Element1,
		Element2:    ring.Element2,
		Element3:    ring.Element3,
		Element4:    ring.Element4,
		LvUpItemNum: ring.LevelUpDelItemNum,
	}

	if err := dB.Save(&ringEntity).Error; err != nil {
		panic(err)
	}

	*ring = buildPbRing(ringEntity)
	return
}

func Delete(dB *gorm.DB, ringIDs []int32) {

	var ringEntity entity.Ring
	if err := dB.Delete(&ringEntity, ringIDs).Error; err != nil {
		panic(err)
	}
}

func buildPbRing(ringEntity entity.Ring) pb.Ring {

	var ring = pb.Ring{
		ID:                ringEntity.ID,
		RingID:            ringEntity.RingID,
		Level:             ringEntity.Level,
		Element1:          ringEntity.Element1,
		Element2:          ringEntity.Element2,
		Element3:          ringEntity.Element3,
		Element4:          ringEntity.Element4,
		LevelUpDelItemNum: ringEntity.LvUpItemNum,
	}

	var epic int32 = 21
	var legend int32 = 26

	if ring.Element1 >= legend && ring.Element2 >= legend && ring.Element3 >= legend && ring.Element4 >= legend {
		ring.UpValue2 = 1000
	} else if ring.Element1 >= epic && ring.Element2 >= epic && ring.Element3 >= epic && ring.Element4 >= epic {
		ring.UpValue2 = 500
	}

	var ringConf, err = configuration.GetByRingID(ring.RingID)
	if err != nil {
		panic(err)
	}

	// 基础属性读属性表的值
	attrConf, err := attribute.GetByAttributeID(ringConf.AttributeID)
	if err != nil {
		panic(err)
	} else {
		ring.Attribute = &pb.Attribute{
			INT: attrConf.INT,
			COR: attrConf.COR,
			CHR: attrConf.CHR,
		}
	}

	upgradeConf, err := upgrade.GetByUpgradeID(ringConf.RefineID)
	if err != nil {
		// 有一些不能重铸
		//panic(err)
	} else {

		// 重铸需要的道具读培养表的值
		ring.RefineItem = &pb.RawItem{
			ItemID: upgradeConf.ItemID,
			Number: upgradeConf.ItemCount,
		}
	}

	upgradeConf, err = upgrade.GetByUpgradeID(ringConf.UpgradeID)
	if err != nil {
		panic(err)
	}

	// 魔戒最大等级
	ring.MaxLevel = upgradeConf.GradeMax

	attrConf, err = attribute.GetByAttributeID(upgradeConf.AttributeID + ring.Level)
	if err != nil {
		panic(err)
	} else {
		// +强化加成读属性表的值 还要除万分比
		ring.Attribute.INT += int32(float64(attrConf.INT) / 10000 * float64(upgradeConf.AttributeRate))
		ring.Attribute.COR += int32(float64(attrConf.COR) / 10000 * float64(upgradeConf.AttributeRate))
		ring.Attribute.CHR += int32(float64(attrConf.CHR) / 10000 * float64(upgradeConf.AttributeRate))

		// +强化需要的道具ID读培养表 需要的道具数量读属性表
		ring.LevelUpItem = &pb.RawItem{
			ItemID: upgradeConf.ItemID,
			Number: attrConf.ItemCost,
		}
	}

	var elements = ring.Element1 + ring.Element2 + ring.Element3 + ring.Element4
	attrConf, err = attribute.GetByAttributeID(upgradeConf.AttributeID + elements)
	if err != nil {
		panic(err)
	} else {
		// 属性加成比例读培养表 (4位数)
		ring.UpValue = attrConf.RingRate
	}

	return ring
}
